package com.iplant.basic.controller.fmc;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.FMCService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.fmc.FMCResource;

@RestController
@RequestMapping("/api/FMCResource")
public class FMCResourceController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(FMCResourceController.class);

    @Autowired
    FMCService wFMCService;

    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            String wResourceCode = StringUtils.parseString(request.getParameter("ResourceCode"));
            String wResourceName = StringUtils.parseString(request.getParameter("ResourceName"));
            int wWorkType = StringUtils.parseInt(request.getParameter("WorkType"));
            int wTypeID = StringUtils.parseInt(request.getParameter("TypeID"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wResourceID = StringUtils.parseInt(request.getParameter("ResourceID"));
            List<Integer> wStationID = StringUtils.parseIntList(request.getParameter("StationID"), ",");
            int wActive = StringUtils.parseInt(request.getParameter("Active"));

            Pagination wPagination = GetPagination(request);
            ServiceResult<List<FMCResource>> wServiceResult = wFMCService.FMC_QueryResourceList(wLoginUser,
                    wResourceCode, wResourceName, wStationID, wWorkType, wTypeID, wProductID, wResourceID, wActive,
                    wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<FMCResource> wServiceResult = wFMCService.FMC_QueryResource(wLoginUser, wID);
            FMCResource wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            FMCResource wFMCResource = CloneTool.Clone(wParam.get("data"), FMCResource.class);
            wFMCResource.EditorName = wUserName;
            wFMCResource.EditorID = wUserID;
            wFMCResource.EditTime = Calendar.getInstance();
            if (wFMCResource.ID <= 0) {

                wFMCResource.CreatorName = wUserName;
                wFMCResource.CreatorID = wUserID;
                wFMCResource.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wFMCService.FMC_UpdateResource(wBMSEmployee, wFMCResource);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wFMCResource);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<FMCResource> wFMCResourceList = CloneTool.CloneArray(wParam.get("data"), FMCResource.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (FMCResource wFMCResource : wFMCResourceList) {
                wFMCResource.setUserInfo(wBMSEmployee);

                wServiceResult = wFMCService.FMC_UpdateResource(wBMSEmployee, wFMCResource);
            }
            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wFMCResourceList, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<FMCResource> wFMCResourceList = CloneTool.CloneArray(wParam.get("data"), FMCResource.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (FMCResource wFMCResource : wFMCResourceList) {

                wServiceResult = wFMCService.FMC_ActiveResource(wBMSEmployee, wFMCResource, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            FMCResource wFMCResource = CloneTool.Clone(wParam.get("data"), FMCResource.class);

            ServiceResult<Integer> wServiceResult = wFMCService.FMC_DeleteResource(wBMSEmployee, wFMCResource);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
